package com.lb.uts.util;

import java.time.*;
import java.util.Date;

public class TimeConvertUtils {
    private static ZoneId zone = ZoneId.systemDefault();

    /**
     * Instant转LocalDateTime
     * @param instant
     * @return
     */
    private static LocalDateTime convertToLocalDateTime(Instant instant){
        return LocalDateTime.ofInstant(instant, zone);
    }

    /**
     * LocalDateTime转Date
     * @param localDateTime
     * @return Date
     */
    public static Date convertToDate(LocalDateTime localDateTime){
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }

    /**
     * LocalDate转Date
     * @param localDate
     * @return
     */
    public static Date convertToDate(LocalDate localDate) {
        Instant instant = localDate.atStartOfDay(zone).toInstant();
        return Date.from(instant);
    }

    /**
     * Date转LocalDate
     * @param date
     * @return localDate
     */
    public static LocalDate convertToLocalDate(Date date){
        Instant instant = date.toInstant();
        return convertToLocalDateTime(instant).toLocalDate();
    }

    /**
     * Date转LocalTime
     * @param date
     * @return localDate
     */
    public static LocalTime convertToLocalTime(Date date){
        Instant instant = date.toInstant();
        return convertToLocalDateTime(instant).toLocalTime();
    }

    /**
     * Date转LocalDatetime
     * @param date
     * @return localDate
     */
    public static LocalDateTime convertToLocalDateTime(Date date){
        Instant instant = date.toInstant();
        return convertToLocalDateTime(instant);
    }
}
